package cc.sunni.user.service.impl;

import cc.sunni.api.utils.PageUtils;
import cc.sunni.api.utils.Query;
import cc.sunni.common.Constant;
import cc.sunni.common.enums.Sex;
import cc.sunni.common.utils.RedisOperator;
import cc.sunni.pojo.user.AppUser;
import cc.sunni.pojo.user.Fans;
import cc.sunni.pojo.vo.RegionRatioVO;
import cc.sunni.user.mapper.FansDao;
import cc.sunni.user.service.AppUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cc.sunni.user.service.FansService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("fansService")
public class FansServiceImpl extends ServiceImpl<FansDao, Fans> implements FansService {
    @Autowired
    private AppUserService userService;
    @Autowired
    private RedisOperator redis;

    @Override
    public boolean isMeFollowThisWriter(String writerId, String fanId) {
        int count = baseMapper.selectCount(new LambdaQueryWrapper<Fans>().eq(Fans::getFanId,fanId).eq(Fans::getWriterId,writerId));
        return count > 0;
    }

    @Override
    @Transactional
    public void follow(String writerId, String fanId) {
        // 获得粉丝用户的信息
        AppUser fanInfo = userService.getUserByUserId(fanId);
        Fans fans = new Fans();
        fans.setFanId(fanId);
        fans.setWriterId(writerId);
        fans.setFace(fanInfo.getFace());
        fans.setFanNickname(fanInfo.getNickname());
        fans.setSex(fanInfo.getSex());
        fans.setProvince(fanInfo.getProvince());
        baseMapper.insert(fans);

        // redis 作家粉丝数累加
        redis.increment(Constant.REDIS_WRITER_FANS_COUNTS + ":" + writerId, 1);
        // redis 当前用户的（我的）关注数累加
        redis.increment(Constant.REDIS_MY_FOLLOW_COUNTS + ":" + fanId, 1);
    }

    @Override
    public void unfollow(String writerId, String fanId) {
        baseMapper.delete(new LambdaQueryWrapper<Fans>().eq(Fans::getFanId,fanId).eq(Fans::getWriterId,writerId));
        // redis 作家粉丝数累减
        redis.decrement(Constant.REDIS_WRITER_FANS_COUNTS + ":" + writerId, 1);
        // redis 当前用户的（我的）关注数累减
        redis.decrement(Constant.REDIS_MY_FOLLOW_COUNTS + ":" + fanId, 1);
    }

    @Override
    public PageUtils queryMyFansList(String writerId, Integer page, Integer pageSize) {
        IPage<Fans> pages = this.page(
                new Page<>(page,pageSize),
                // 拼接查询条件
                new LambdaQueryWrapper<Fans>().eq(Fans::getWriterId,writerId)
        );
        return new PageUtils(pages);
    }

    @Override
    public Integer queryFansCounts(String writerId, Sex sex) {
        return baseMapper.selectCount(new LambdaQueryWrapper<Fans>().eq(Fans::getWriterId,writerId).eq(Fans::getSex,sex.type));
    }

    @Override
    public List<RegionRatioVO> queryRegionRatioCounts(String writerId) {
        List<RegionRatioVO> list = new ArrayList<>();
        for (String r : regions) {
            Integer count = baseMapper.selectCount(new LambdaQueryWrapper<Fans>().eq(Fans::getWriterId,writerId).eq(Fans::getProvince,r));
            RegionRatioVO regionRatioVO = new RegionRatioVO();
            regionRatioVO.setName(r);
            regionRatioVO.setValue(count);
            list.add(regionRatioVO);
        }
        return list;
    }

    public static final String[] regions = {"北京", "天津", "上海", "重庆",
            "河北", "山西", "辽宁", "吉林", "黑龙江", "江苏", "浙江", "安徽", "福建", "江西", "山东",
            "河南", "湖北", "湖南", "广东", "海南", "四川", "贵州", "云南", "陕西", "甘肃", "青海", "台湾",
            "内蒙古", "广西", "西藏", "宁夏", "新疆",
            "香港", "澳门"};

}